var loginUrl = "/view/login";

function getHeaders() {
    const headers = [];
    const ti = layui.admin.getTempData("tokenInfo", true);
    if (!ti) {
        return headers;
    }
    headers.push({name: ti.tokenName, value: 'Bearer ' + ti.tokenValue});
    return headers;
}

function noLogin(msg) {
    layui.notice.error({
        timeout: 1500,
        title: msg,
        message: '即将跳转至登录页.....'
    });
    setTimeout(function () {
        top.location.replace(loginUrl);
        console.log("ajax未登录")
    }, 1000);

    return false;
}

setTableHeaders();
function setTableHeaders() {

}

layui.config({  // common.js是配置layui扩展模块的目录，每个页面都需要引入
    version: '620',   // 更新组件缓存，设为true不缓存，也可以设一个固定值
    base: getProjectUrl() + '/static/module/',
    // defaultTheme: 'theme-colorful',
    cacheTab: false,                // 刷新页面不恢复已经打开的Tab
    maxTabNum: 20,                  // 最大打开的Tab数量
    openTabCtxMenu: true,          // 关闭Tab鼠标右键菜单
    tableName: 'ai-sys',    // 存储表名
    closeFooter: true,              // 是否关闭页脚
    tabAutoRefresh: true,           // 是否切换Tab自动刷新页面
    getAjaxHeaders: function () {
        return getHeaders();
    },
    ajaxSuccessBefore: function (res, url, obj) {
        if (res.code === 499) {
            noLogin(res.msg);
        }
        return true;
    }
}).extend({
    steps: 'steps/steps',
    notice: 'notice/notice',
    cascader: 'cascader/cascader',
    dropdown: 'dropdown/dropdown',
    fileChoose: 'fileChoose/fileChoose',
    Split: 'Split/Split',
    Cropper: 'Cropper/Cropper',
    tagsInput: 'tagsInput/tagsInput',
    citypicker: 'city-picker/city-picker',
    iconPicker: 'iconPicker/iconPicker',
    introJs: 'introJs/introJs',
    zTree: 'zTree/zTree',
}).use(['layer', 'admin', 'notice', 'table'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var admin = layui.admin;
    var table = layui.table;
    var notice = layui.notice;
    /*全局调用*/
    var ws;
    let header = getHeaders()[0];

    let tempData = admin.getTempData("tokenInfo", true);
    if (tempData) {
        admin.authList = tempData.perms;
    }
    /** 判断是否有权限 */
    admin.hasPerm = function (p) {
        if (admin.authList) for (var i = 0; i < admin.authList.length; i++) if ("*.*.*" == admin.authList[i] || p == admin.authList[i]) return true;
        return false;
    };
    /** 移除没有权限的元素 */
    admin.renderPerm = function () {
        $('[perm-show]').each(function () {
            if (!admin.hasPerm($(this).attr('perm-show'))) {
                $(this).addClass('layui-hide')
                $(this).remove();
                console.log('无')
            } else {
                $(this).removeClass('layui-hide')
                console.log('有')
            }
        });
    };
    admin.renderPerm();

    if(header){
        $.ajaxSetup({
            beforeSend:function (xhr) {
                xhr.setRequestHeader(header.name,header.value);
            },
            ajaxComplete:function (xhr) {
                if(xhr.code === 499){
                    noLogin(xhr.msg)
                }
            }
        })
        table.set({
            headers: {[header.name]:header.value},
            parseData: function (res) {  // 利用parseData实现预处理
                if (res.code === 499) {
                    noLogin(res.msg);
                }
                return res;
            }
        });
    }

});


/** 获取当前项目的根路径，通过获取layui.js全路径截取assets之前的地址 */
function getProjectUrl() {
    var layuiDir = layui.cache.dir;
    if (!layuiDir) {
        var js = document.scripts, last = js.length - 1, src;
        for (var i = last; i > 0; i--) {
            if (js[i].readyState === 'interactive') {
                src = js[i].src;
                break;
            }
        }
        var jsPath = src || js[last].src;
        layuiDir = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
    }
    return layuiDir.substring(0, layuiDir.indexOf('/static'));
}

